package com.network.retrofit.net.http;

import android.net.ParseException;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.URL;

import retrofit2.HttpException;


/**
 * 项目创建人:
 * 创建人:fargo(罗)
 * Created by app on 2020/9/15  —————— 9:58
 * 作用:
 * 备注: http错误工具类
 **/
public class ExceptionHandle {


    public static ResponseThrowable handleException(Throwable e) {
        ResponseThrowable ex=null;
        try {
            if (e instanceof HttpException) {
                HttpException httpException = (HttpException) e;
                ex = new ResponseThrowable(e, ERROR.HTTP_ERROR);
                URL url=httpException.response().raw().networkResponse().request().url().url();
                if(url!=null) {
                    ex.setResponseUrlContent(url.toString());
                }
                if(HttpExceptionCode.HttpExceptionCodeDetails.containsKey(httpException.code())){
                    ex.message =HttpExceptionCode.HttpExceptionCodeDetails.get(httpException.code());
                }else{
                    ex.message = "其他网络错误";
                }
                return ex;
            } else if (e instanceof ServerException) {
                ServerException resultException = (ServerException) e;
                ex = new ResponseThrowable(resultException, resultException.code);
                ex.message = resultException.message;
                return ex;
            } else if (e instanceof JsonParseException
                    || e instanceof JSONException
                    || e instanceof ParseException) {
                ex = new ResponseThrowable(e, ERROR.PARSE_ERROR);
                ex.message = "解析错误";
                return ex;
            } else if (e instanceof ConnectException) {
                ex = new ResponseThrowable(e, ERROR.NETWORD_ERROR);
                ex.message = "连接失败";
                return ex;
            } else if (e instanceof javax.net.ssl.SSLHandshakeException) {
                ex = new ResponseThrowable(e, ERROR.SSL_ERROR);
                ex.message = "证书验证失败";
                return ex;
            } else if (e instanceof java.net.SocketTimeoutException) {
                ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
                //ex.message = "连接超时";
                ex.message = "当前网络连接不顺畅，请稍后再试！";
                return ex;
            } else if (e instanceof java.net.UnknownHostException) {
                ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
                ex.message = "网络中断，请检查网络状态！";
                return ex;
            } else if (e instanceof javax.net.ssl.SSLException) {
                ex = new ResponseThrowable(e, ERROR.TIMEOUT_ERROR);
                ex.message = "网络中断，请检查网络状态！";
                return ex;
            } else if (e instanceof java.io.EOFException) {
                ex = new ResponseThrowable(e, ERROR.PARSE_EmptyERROR);
                ex.message = "1007";
                return ex;
            } else if (e instanceof NullPointerException) {
                ex = new ResponseThrowable(e, ERROR.PARSE_EmptyERROR);
                ex.message = "数据为空，显示失败";
                return ex;
            } else {
                ex = new ResponseThrowable(e, ERROR.UNKNOWN);
                ex.message = "未知错误";
                return ex;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            return ex;
        }
    }


    /**
     * 约定异常
     */
    public class ERROR {
        /**
         * 未知错误
         */
        public static final int UNKNOWN = 1000;
        /**
         * 解析错误
         */
        public static final int PARSE_ERROR = 1001;
        /**
         * 解析no content错误
         */
        public static final int PARSE_EmptyERROR = 1007;
        /**
         * 网络错误
         */
        public static final int NETWORD_ERROR = 1002;
        /**
         * 协议出错
         */
        public static final int HTTP_ERROR = 1003;

        /**
         * 证书出错
         */
        public static final int SSL_ERROR = 1005;

        /**
         * 连接超时
         */
        public static final int TIMEOUT_ERROR = 1006;

        public static final int LOGIN_ERROR = -1000;
        public static final int DATA_EMPTY = -2000;


    }


    public class ServerException extends RuntimeException {
        public int code;
        public String message;

        public ServerException(int code, String message) {
            this.code = code;
            this.message = message;
        }
    }
}

